<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function f() {
            document.getElementById('unameMsg').innerHTML=""
            document.getElementById('upswMsg').innerHTML=""
            var ele = document.getElementById('uname');
            if (ele.value==""){
                document.getElementById('unameMsg').innerHTML="用户名不能为空"
            }
            var ele1 = document.getElementById('upsw');
            if (ele1.value==""){
                document.getElementById('upswMsg').innerHTML="密码不能为空"
            }
        }
    </script>
</head>
<body onchange="f(this.value)" >
用户名：<input type="text" id="uname" value="">
<font id="unameMsg" color="red">请输入用户名</font>
<br>

密码：<input type="password" id="upsw" value="">
<font  id="upswMsg" color="red">请输入密码</font>
<br>
</body>
</html>